package mysql

import (
	"context"
	"go.uber.org/zap"
	"golang-sgg-daijia/internal/model"
)

type DriverSetDao interface {
	Create(ctx context.Context, driverSet *model.DriverSet) error
	GetByDriverID(ctx context.Context, driverID int64) (*model.DriverSet, error)
	UpdateServiceStatus(ctx context.Context, driverID int64, status int) (bool, error)
}

type driverSetDao struct {
	*MysqlClient
}

func NewDriverSetDao(
	r *MysqlClient,
) DriverSetDao {
	return &driverSetDao{
		MysqlClient: r,
	}
}

func (r *driverSetDao) Create(ctx context.Context, driverSet *model.DriverSet) error {
	if err := r.DB(ctx).Create(driverSet).Error; err != nil {
		r.logger.WithContext(ctx).Error("customerLoginLogDao|Create(", zap.Any("data", map[string]interface{}{
			"driverSet": *driverSet,
		}), zap.Error(err))
		return err
	}
	return nil
}

func (r *driverSetDao) GetByDriverID(ctx context.Context, driverID int64) (*model.DriverSet, error) {
	var driverSet model.DriverSet
	if err := r.DB(ctx).Where("driver_id = ?", driverID).First(&driverSet).Error; err != nil {
		r.logger.WithContext(ctx).Error("driverSetDao|GetDriverSet(", zap.Any("data", map[string]interface{}{
			"driverID": driverID,
		}), zap.Error(err))
		return nil, err
	}
	return &driverSet, nil
}

func (r *driverSetDao) UpdateServiceStatus(ctx context.Context, driverID int64, status int) (bool, error) {
	err := r.DB(ctx).Model(&model.DriverSet{}).Where("driver_id = ?", driverID).Update("service_status", status).Error
	if err != nil {
		r.logger.WithContext(ctx).Error("driverSetDao|UpdateServiceStatus(", zap.Any("data", map[string]interface{}{
			"driverID": driverID,
			"status":   status,
		}), zap.Error(err))
		return false, err
	}
	return true, nil
}